package ${package.Controller};

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.web.bind.annotation.*;

<#if restControllerStyle>
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
<#else>
import org.springframework.stereotype.Controller;
</#if>
<#if superControllerClassPackage??>
import ${superControllerClassPackage};
</#if>
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import ${package.Service}.${table.serviceName};
import ${package.Entity}.${table.entityName};
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import io.swagger.annotations.ApiOperation;

import java.util.List;
import java.util.Map;
/**
 * ${table.name} ${table.comment}
 * @author ${author}
 * @date ${date}
 * @version V1.0.0
 */
@Slf4j
<#if restControllerStyle>
@RestController
<#else>
@Controller
</#if>
@RequestMapping("<#if package.ModuleName?? && package.ModuleName != "">/${package.ModuleName}</#if>/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>")
<#if kotlin>
class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}()</#if>
<#else>
<#if superControllerClass??>
public class ${table.controllerName} extends ${superControllerClass} {
<#else>
public class ${table.controllerName} {
</#if>
    /**
     * ${table.comment}业务接口
     */
    @Autowired
    private ${table.serviceName} ${table.serviceName?uncap_first};

    /**
    * 列表查询
    * @author ${author}
    * @date ${date}
    * @version V1.0.0
    * @param entity 条件
    * @return List<${table.entityName}>集合
    */
    @ApiOperation(value="${table.name}-列表查询", notes="${table.name}-列表查询")
    @GetMapping("/list")
    public Result list(@RequestBody ${table.entityName} entity) {
        LambdaQueryWrapper<${table.entityName}> queryWrapper = this.initQueryWrapper(entity);
        List<${table.entityName}> list = ${table.entityName?uncap_first}Service.list(queryWrapper);
        return Result.OK(list);
    }
    /**
     * 条件
     */
    private LambdaQueryWrapper initQueryWrapper(${table.entityName} entity){
        LambdaQueryWrapper<${table.entityName}> queryWrapper = new LambdaQueryWrapper<>();
        //自定义条件
        return queryWrapper;
    }

    /**
    * 分页列表查询
    * @author ${author}
    * @date ${date}
    * @version V1.0.0
    * @param entity 条件
    * @param pageNo 页数
    * @param pageSize 条数
    * @return page 分页数据
    */
    @ApiOperation(value="${table.name}-分页列表查询", notes="${table.name}-分页列表查询")
    @GetMapping("/pageList")
    public Result pageList(${table.entityName} entity,
                            @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                            @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
        pageSize = 100 < pageSize ? 100 : pageSize;
        LambdaQueryWrapper<${table.entityName}> queryWrapper = this.initQueryWrapper(entity);
        Page<${table.entityName}> page = new Page<>(pageNo, pageSize);
        IPage<${table.entityName}> pageList = ${table.entityName?uncap_first}Service.page(page, queryWrapper);
        return Result.OK(pageList);
    }

<#--    /**-->
<#--    * 根据id获取单个实体-->
<#--    * @author ${author}-->
<#--    * @date ${date}-->
<#--    * @version V1.0.0-->
<#--    * @param id 主键-->
<#--    * @return ${table.entityName?uncap_first} 对象实体-->
<#--    */-->
<#--    @ApiOperation(value="${table.name}-根据id获取单个实体", notes="${table.name}-根据id获取单个实体")-->
<#--    @GetMapping("/getById/{id}")-->
<#--    public Result getById(@PathVariable String id) {-->
<#--        if(null == id ){-->
<#--            return Result.error("id不能为空");-->
<#--        }-->
<#--        ${table.entityName} ${table.entityName?uncap_first} = ${table.entityName?uncap_first}Service.getById(id);-->
<#--        return Result.OK(${table.entityName?uncap_first});-->
<#--    }-->


    /**
    * 添加
    * @author ${author}
    * @date ${date}
    * @version V1.0.0
    * @param entity 对象参数
    * @return Result 成功/失败
    */
    @ApiOperation(value="${table.name}-添加", notes="${table.name}-添加")
    @PostMapping("/add")
    public Result add(@RequestBody ${table.entityName} entity) {
        try{
            if(${table.entityName?uncap_first}Service.save(entity)){
                return Result.OK();
            }
        }catch(Exception e){
            e.printStackTrace();
        }
         return Result.error("添加失败！");
    }

    /**
    * 编辑
    * @author ${author}
    * @date ${date}
    * @version V1.0.0
    * @param entity 对象参数
    * @return Result 成功/失败
    */
    @ApiOperation(value="${table.name}-编辑", notes="${table.name}-编辑")
    @PostMapping("/edit")
    public Result edit(@RequestBody ${table.entityName} entity) {
        try{
            if(${table.entityName?uncap_first}Service.updateById(entity)){
                return Result.OK();
            }
        }catch(Exception e){
            e.printStackTrace();

        }
        return Result.error("编辑失败!");
    }

    /**
    * 物理删除
    * @author ${author}
    * @date ${date}
    * @version V1.0.0
    * @param param 删除的id集合
    * @return Result 成功/失败
    */
    @ApiOperation(value="${table.name}-根据id删除单个实体", notes="${table.name}-根据id删除单个实体")
    @PostMapping("/del")
    public Result del(@RequestBody Map<String,List> param) {
        try{
            if(!param.containsKey("ids")){
                return Result.error("ids不能为空");
            }
<#--            if(${table.entityName?uncap_first}Service.removeByIds(param.get("ids"))){-->
<#--                return Result.OK();-->
<#--            }-->

<#--            逻辑删除-->
            UpdateWrapper<${table.entityName}> updateWrapper =new UpdateWrapper<>();
            updateWrapper.set("is_delete",1);
            updateWrapper.in("id",param.get("ids"));
            if(${table.entityName?uncap_first}Service.update(updateWrapper)){
                return Result.OK();
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return Result.error("删除失败!");
    }

}
</#if>
